define(function (require, exports, module) {
    var $ = require("jquery");
    require("pagination");
    var util = require("util");
    var ui = require("ui");
    var angular = require("angular");
    var api_prefix = G_CONFIG["api_prefix"] + 'qmyx'; //api前缀

    var ditchSourceModule = angular.module('ditchSourceModule', []);

    function ditchSourceService($http, $q, $location) {
        var service = {
            ditchSources: [],//ditchSources列表
            filter: {pageSize: 10},

            initData: function () {
                var p = service.filter;
                service.query(p.pageIndex || 1, p.pageSize || 10, p.keyword || '');
            },
            query: function (pageIndex, pageSize, keyword) {
                service.filter = {pageIndex: pageIndex, pageSize: pageSize, keyword: keyword};
                $http.get(api_prefix + '/channel-source/get-channel-source-list', {params: service.filter})
                    .then(function (res) {
                        var retCode = res.data.retCode;
                        if (retCode == 0) {
                            var data = res.data.data;
                            service.ditchSources = data.items;
                            service.total = data.total;
                            service.filter.pageIndex = pageIndex;
                        } else {
                            ui.showErrTip(res.data.errMsg);
                            return false;
                        }
                    });
            },
            // 初始化查询表单数据
            initEditSourceDialog: function ($data, $dialog, $dialogScope, ditchsource) {
                $dialogScope.setFontColor = function(color){
                    $data.font_color = color;
                }
                $dialogScope.setBtnColor = function(color){
                    $data.button_color = color;
                }
                $(document).on('click',function(e){
                   if(!$(e.target).closest('.font-board,.color-picker').length){
                       $dialogScope.showFontColorPicker = false;
                       $dialogScope.showBtnColorPicker = false;
                       $dialogScope.$applyAsync();
                   }
                });

                angular.extend($data, ditchsource);

                //上传二维码内部图片
                $dialogScope.uploadQRImages = function (status) {
                    switch (status.status) {
                        case 'error':
                            if (status.msg.toString().match(/File\s+size\s+error/)) {
                                $dialogScope.uploader.stateqr = '图片大小超出限制';
                            } else if (status.msg.toString().match(/File\s+extension\s+error/)) {
                                $dialogScope.uploader.stateqr = '图片类型不符';
                            }
                            else {
                                $dialogScope.uploader.stateqr = status.msg;
                            }

                            break;
                        case 'success':
                            //获取二维码图片
                            setTimeout(function(){
                                var parms = {qr_code_innner_img:$data.qr_code_innner_img};
                                $http.get(api_prefix + '/channel-source/crete-qr-code-img-url', {params: parms})
                                    .then(function (res) {
                                        var retCode = res.data.retCode;
                                        if (retCode == 0) {
                                            $data.qr_code_img = res.data.data;
                                        } else {
                                            ui.showErrTip(res.data.errMsg);
                                            return false;
                                        }
                                    });
                            }, 10);

                            break;
                    }
                }

            },
            initViewFunsDialog: function ($data, $dialog, $dialogScope, ditchsource){

            },
            initViewBrokerDialog: function ($data, $dialog, $dialogScope, ditchsource){},
            save: function ($data, $dialog) {
                var data = {modelData: angular.extend({}, $data)};
                var url = api_prefix + "/channel-source/save-channel-source";

                util.myAjax({
                    url: url,
                    type: "POST",
                    data: data,
                    context: this,
                    success: function (json) {
                        if (json.retCode == 0) {
                            service.initData();
                            ui.showSucTip();
                            $dialog.close();
                        } else {
                            ui.showErrTip(json.errMsg);
                        }
                    }
                });
                return false;
            },
            delete: function (ditchsource) {
                util.confirm('确定要删除此Source吗?', {
                    ok: function () {
                        var data = {ditchsourceId: ditchsource.ditchsource_id};
                        var url = api_prefix + "/rclc/ditchsource/delete-ditchsource-by-id";
                        util.myAjax({
                            url: url,
                            type: "POST",
                            data: data,
                            context: this,
                            success: function (json) {
                                if (json.retCode == 0) {
                                    service.initData();
                                    ui.showSucTip();
                                } else {
                                    ui.showErrTip(json.errMsg);
                                }
                            }
                        });
                    }
                });
            }
        };

        return service;
    }

    ditchSourceModule.factory('ditchSourceSvr', ['$http', '$q', '$location', ditchSourceService]);

    module.exports = ditchSourceModule;
});